//
//  EntrepreneurDetailViewController.m
//  BigToBig
//
//  Created by liuyi on 7/19/16.
//  Copyright © 2016 cn.cloudtp. All rights reserved.
//

#import "EntrepreneurDetailViewController.h"
#import "EntrepreneurDetailView.h"
#import "DetailInfoCell.h"
#import "DetailConceptsCell.h"
#import "DetailMemberCell.h"
#import "DetailMessageCell.h"
#import "DetailFollowerCell.h"
#import "CreateRoadShowViewController.h"

@interface EntrepreneurDetailViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>
@property (nonatomic , retain)EntrepreneurDetailView *entrepreneurDetailView;
@property (nonatomic , retain)UITableView *mainTableView;


@property (nonatomic , retain)NSArray *sectionTitleArray;
@property (nonatomic , retain)NSString *info;
@property (nonatomic , retain)NSDictionary *concepts;
@property (nonatomic , retain)NSArray *conceptsKey;
@end

@implementation EntrepreneurDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createData];
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLab.text = self.model.project_name;
    titleLab.textColor = [UIColor whiteColor];
    [self setBackBar];
    titleLab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLab;
    
    [self createView];    
}

#pragma mark - 占位数据
- (void)createData{
    self.sectionTitleArray = @[@"项目简介",@"概念阶段",@"团队成员",@"融资信息",@"项目导师",@"项目关注人"];
    self.info = self.model.product_introduction;
    self.conceptsKey = @[@"竞争优势",@"商业模式",@"市场调研"];
    self.concepts = [NSDictionary dictionaryWithObjectsAndKeys:self.model.competitive_edge,self.conceptsKey[0],self.model.business_model,self.conceptsKey[1],self.model.market_research,self.conceptsKey[2],nil];
}

#pragma mark 导航栏左右Button
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    
}

- (void)backController:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createView{
    // 创建头视图
    self.entrepreneurDetailView = [[EntrepreneurDetailView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
    [self.entrepreneurDetailView.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.bigtobig.com/%@",self.model.project_logo]]];
    
    // 头视图
    self.entrepreneurDetailView.funsLabel.text = [NSString stringWithFormat:@"  粉丝 : %@",self.model.saw_count];
    self.entrepreneurDetailView.focusLabel.text = [NSString stringWithFormat:@"  已关注 : %@",self.model.notice_count];
    NSArray *markArr = [NSMutableArray array];
    markArr = [self.model.areas componentsSeparatedByString:@"|"];
    self.entrepreneurDetailView.markArray = markArr.mutableCopy;
    self.entrepreneurDetailView.titleLabel.text = self.model.project_name;
    if(self.model.project_progress.integerValue == 0){
        self.entrepreneurDetailView.groupLabel.text = @"未完成";

    }
    else{
        self.entrepreneurDetailView.groupLabel.text = @"已完成";

    }
    [self.entrepreneurDetailView.markCollectionView reloadData];

    // 主tableView
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 50) style:UITableViewStyleGrouped];
    self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, -70, 0);
    self.mainTableView.tableHeaderView = self.entrepreneurDetailView;
    [self.view addSubview:_mainTableView];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.rowHeight = 70;
    self.mainTableView.sectionHeaderHeight = 30;
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    [self.mainTableView registerClass:[DetailInfoCell class] forCellReuseIdentifier:NSStringFromClass([DetailInfoCell class])];
    [self.mainTableView registerClass:[DetailConceptsCell class] forCellReuseIdentifier:NSStringFromClass([DetailConceptsCell class])];
    [self.mainTableView registerClass:[DetailMemberCell class] forCellReuseIdentifier:NSStringFromClass([DetailMemberCell class])];
    [self.mainTableView registerClass:[DetailMessageCell class] forCellReuseIdentifier:NSStringFromClass([DetailMessageCell class])];
    [self.mainTableView registerClass:[DetailFollowerCell class] forCellReuseIdentifier:NSStringFromClass([DetailFollowerCell class])];
    self.mainTableView.bounces = NO;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight - 50, kScreenWidth, 50)];
//    self.mainTableView.tableFooterView = footerView;
    [self.view addSubview:footerView];
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeSystem];
    editButton.frame = CGRectMake(0, 0, kScreenWidth / 2, 50);
    [editButton setTitle:@"编辑内容" forState:UIControlStateNormal];
    [editButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    editButton.backgroundColor = [UIColor whiteColor];
    [editButton addTarget:self action:@selector(editButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:editButton];
    
    UIButton *createButton = [UIButton buttonWithType:UIButtonTypeSystem];
    createButton.backgroundColor = COLOR_RGBA(255, 81, 90, 1);
    createButton.frame = CGRectMake(kScreenWidth / 2, 0, kScreenWidth / 2, 50);
    [createButton setTitle:@"创建路演" forState:UIControlStateNormal];
    [createButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [createButton addTarget:self action:@selector(createButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:createButton];

}

#pragma mark - Button 点击方法

- (void)editButtonAction:(UIButton *)button{
    
}

- (void)createButtonAction:(UIButton *)button{
    CreateRoadShowViewController *createRoadShowVC = [[CreateRoadShowViewController alloc]init];
    
    [self.navigationController pushViewController:createRoadShowVC animated:YES];
    
    
}

#pragma mark - mainTableView代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    else if(section == 1){
        return 3;
    }
    else if (section == 2){
        return 3;
    }
    else if (section == 3){
        return 1;
    }
    else if (section == 4){
        return 2;
    }else if (section == 5){
        return 1;
    }
    else {
        return 0;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    UILabel *sectionTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.entrepreneurDetailView.maxY, 30)];
    sectionTitle.text = self.sectionTitleArray[section];
    sectionTitle.font = [UIFont systemFontOfSize:15];
    [sectionView addSubview:sectionTitle];
    return sectionView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        DetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailInfoCell class]) forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.infoLabel.text = self.info;
        return cell;
    }
    else if (indexPath.section == 1){

        DetailConceptsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailConceptsCell class]) forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        NSString *concepts = [self.concepts objectForKey:self.conceptsKey[indexPath.row]];
        cell.conceptsLabel.text = concepts;
        cell.conceptsKeyLabel.text = self.conceptsKey[indexPath.row];

        return cell;
    }
    else if (indexPath.section == 2){
        DetailMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailMemberCell class]) forIndexPath:indexPath];
        cell.titleLabel.text = @"那那那";
        cell.describeLabel.text = @"纯天然的无污染的.大大学生清华北大毕业,一本二本三本创作了什么项目呢哈哈哈,哈哈哈黄飞鸿金";
        return cell;
    }
    else if (indexPath.section == 3){
        DetailMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailMessageCell class]) forIndexPath:indexPath];
        if (self.model.project_process.integerValue == 1) {
            cell.messageTotalLabel.text = @"0万";
            cell.messageStockLabel.text = @"0%";
            cell.messageRaiseLabel.text = @"0份";
            NSString *fundUse = @"无";
            cell.fundUseLabel.text = [NSString stringWithFormat:@"资金用途 : %@",fundUse];
            NSString *coreRequirement = @"无";
            cell.coreRequirementLabel.text = [NSString stringWithFormat:@"资金外核心需求 : %@",coreRequirement];
        }
        else{
            cell.messageTotalLabel.text = [NSString stringWithFormat:@"%@万",self.model.finance_account];
            cell.messageStockLabel.text = [NSString stringWithFormat:@"%@ %",self.model.sell_stock];
            cell.messageRaiseLabel.text = [NSString stringWithFormat:@"%@ 份",self.model.finace_number];
            cell.fundUseLabel.text = [NSString stringWithFormat:@"资金用途 : %@",self.model.fund_use];
            cell.coreRequirementLabel.text = [NSString stringWithFormat:@"资金外核心需求 : %@",self.model.other_needs];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
    else if(indexPath.section == 4){
        DetailMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailMemberCell class]) forIndexPath:indexPath];
        cell.titleLabel.text = @"那那那";
        cell.describeLabel.text = @"纯天然的无污染的.大大学生清华北大毕业,一本二本三本创作了什么项目呢哈哈哈,哈哈哈黄飞鸿金";
        return cell;

    }
    else if (indexPath.section == 5){
        DetailFollowerCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailFollowerCell class]) forIndexPath:indexPath];
        return cell;
    }
    else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        // 计算文字的高度
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
        // 根据文本的内容和文本的字体进行计算高度
        // 参数一:告诉系统,文本显示的最大范围
        CGFloat width = (self.view.frame.size.width - 40);
        CGRect rect = [self.info boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        // 然后把图片的高和文本的高作为返回值返回
        return rect.size.height + 20;
    }
    else if (indexPath.section == 1){
        return 60;
    }
    else if (indexPath.section == 2){
        return 100;
    }
    else if (indexPath.section == 3){
        return 240;
    }
    else if (indexPath.section == 4){
        return 100;
    }
    else if (indexPath.section == 5){
        return 140;
    }
    else{
        return 50;
    }
}

- (void)setModel:(CreatedProjectModel *)model{
    _model = model;
    [self.mainTableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
